<script setup>
  import { ref } from 'vue'
  import { getFileList } from '@/api/file'
  const uploadPopup = ref(null)
  const pictureList = ref([])
  const queryData = ref({
    pageNum: 1,
    pageSize: 10,
    type: 'image'
  })
  getFileList(queryData.value).then(res => {
    pictureList.value = res.data.records
  })
  function open() {
    uploadPopup.value.open()
  }
  function close() {
    uploadPopup.value.close()
  }
  function select(picture) {
    emit('select', picture)
    close()
  }
  const emit = defineEmits(['select'])
  defineExpose({ open, close })
  const loading = ref(false)
  function handleUpload(data) {
    loading.value = false
    ElMessage.success('上传成功')
    pictureList.value.unshift(data)
  }
</script>

<template>
  <Popup ref="uploadPopup">
    <div class="popup" v-loading="loading" element-loading-text="上传中...">
      <div class="header flex-between">
        <div><span class="line"></span> <span>选择图片</span></div>
        <div @click="close" class="pointer">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-quxiao"></use>
          </svg>
        </div>
      </div>
      <div class="body">
        <div class="box">
          <div class="img">
            <Upload @success="handleUpload"></Upload>
          </div>
          <div class="img" v-for="picture in pictureList" :key="picture.id">
            <el-image :src="picture.path" fit="cover" lazy />
            <div class="mask flex-center" @click="select(picture)">
              <svg class="icon flex" aria-hidden="true">
                <use xlink:href="#icon-queren"></use>
              </svg>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Popup>
</template>

<style lang="scss" scoped>
  $img-width: 95px;
  .popup {
    width: 60vw;
    height: 60vh;
    padding: 30px;
    border-radius: 30px;
    background: var(--color-background);
    max-width: 1520px;
    .header {
      font-weight: bold;
      margin-bottom: 10px;
      .line {
        background: var(--el-color-primary);
        position: relative;
        top: 2px;
        display: inline-block;
        width: 6px;
        height: 16px;
        border-radius: 3px;
      }
    }
    .body {
      overflow: scroll;
      height: 90%;
      .box {
        margin-top: 10px;
        display: grid;
        justify-content: space-between;
        grid-template-columns: repeat(auto-fill, $img-width);
        gap: 10px;
        .img {
          width: $img-width;
          position: relative;
          font-size: 11px;
          cursor: pointer;
          .mask {
            display: none;
            color: #fff;
            position: absolute;
            top: 0;
            left: 0;
            width: $img-width;
            height: $img-width;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1;
            transition: opacity 0.3s;
            border-radius: 5px;
          }
          &:hover {
            .mask {
              display: flex;
            }
          }
        }
        .el-image {
          display: block;
          width: $img-width;
          height: $img-width;
          border-radius: 5px;
          overflow: hidden;
        }
      }
    }
  }
  @media screen and (max-width: 768px) {
    .popup {
      width: 100vw;
    }
  }
</style>
